package com.hdoit.ares.logical.acc.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.hdoit.ares.logical.acc.dao.IAwardSettingDao;
import com.hdoit.ares.logical.acc.model.AwardSetting;
import com.hdoit.ares.logical.viewmodel.acc.AwardSettingSearch;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月28日 19:30:47
 * @comment
 */
@Component
public class AwardSettingDaoImpl extends SuperDao<AwardSetting> implements IAwardSettingDao{

	@Override
	public List<AwardSetting> queryList(AwardSettingSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
		                .from(AwardSetting.class)
		                .andWhere("deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no)
		                .addOrder("helpCode", OrderEnum.asc);
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	private void appendFinder(AwardSettingSearch search, IFinderExpression finder) {
		if(!StringUtils.isEmpty(search.getAwardName())){
			finder.andWhere("awardName like #{awardName}")
				  .setParam("awardName", "%"+search.getAwardName()+"%");
		}
		
		if(!StringUtils.isEmpty(search.getHelpCode())){
			finder.andWhere("helpCode like #{helpCode}")
				  .setParam("helpCode", "%"+search.getHelpCode()+"%");
		}
		
		if(!StringUtils.isEmpty(search.getInstruction())){
			finder.andWhere("instruction like #{instruction}")
				  .setParam("instruction", "%"+search.getInstruction()+"%");
		}
		
	}

	@Override
	public List<AwardSetting> queryList(AwardSettingSearch search) {
		return queryList(search,null);
	}

	@Override
	public int queryCount(AwardSettingSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT id) as count")
		                .from(AwardSetting.class)
		                .andWhere("deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no);
		
		appendFinder(search,finderExpression);
		return queryCount(finderExpression, search);
	}

	@Override
	public AwardSetting queryModelByHC(String helpCode,String insuranceType) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select(" * ")
                        .from(AwardSetting.class)
                        .andWhere("helpCode=#{helpCode}")
                        .setParam("helpCode", helpCode)
                        .andWhere("deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no)
		                .andWhere("insuranceType = #{insuranceType}")
		                .setParam("insuranceType", insuranceType);
		return queryModel(finderExpression);
	}

}
